package com.chinaunicom.emergency.repository;

import com.chinaunicom.emergency.model.YjglUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author liliang
 * @data 2019-12-13
 */
@Repository
public interface UsersRepository extends JpaRepository<YjglUser, String>, JpaSpecificationExecutor {
    //根据用户id查询用户信息
    YjglUser findYjglUserByPidAndIsSoftDel(String userId, Integer isSoftDel);

    YjglUser findYjglUserByPidAndUserNameAndIsSoftDel(String userId, String userName, Integer isSoftDel);



    List<YjglUser> findByOrgIdInAndIsSoftDel(List<String> orgId, Integer isSoftDel);

    //根据名模糊插询列表
    List<YjglUser> findByRealNameLike(String realName);

    @Query(nativeQuery = true, value = "select * from YJGL_USER where REAL_NAME like %?1% and IS_SOFT_DEL = ?2")
    List<YjglUser> findUserByRealName(String memberId, Integer i);

    YjglUser findYjglUserByUserNameAndIsSoftDel(String userName, Integer i);

    @Query(nativeQuery = true, value = "select USER_NAME from YJGL_USER where org_id in (?1)")
    List<String> getYjglUsersIdByOrgId(List orgId);

    @Query(nativeQuery = true, value = "select ORG_ID,USER_NAME from YJGL_USER where IS_SOFT_DEL = 0")
    List<Object[]> getAllUsers();

    @Query(nativeQuery = true, value = "select USER_NAME from YJGL_USER where org_id=?1")
    List<String> getYjglUsersIdByOrgId(String orgId);

    List<YjglUser> findAllByIsSoftDel(Integer isSoftDel);

    @Query(nativeQuery = true,value = "select count(1) from YJGL_USER where ORG_ID = ?1 and IS_SOFT_DEL = ?2")
    Long countUserOfDeptId(String orgId, Integer i);

    List<YjglUser> findByUserNameIn(List<String> userNamesList);

    YjglUser findByUserNameAndIsSoftDel(String userName, int i);

    List<YjglUser> findByUserNameInAndIsSoftDel(List<String> userNamesList, int i);

    @Query(nativeQuery = true,value = "select max(ORDER_NUM) from YJGL_USER")
    Integer getMaxOrderNum();

    List<YjglUser> findByOrgIdAndIsSoftDel(String orgId, int i);

    @Query(nativeQuery = true, value = "select PID from YJGL_USER where IS_SOFT_DEL = ?1 " +
            "and (DICT_NATION = ?2 or DICT_DEGREE = ?2 or DICT_UNIT_NATURE = ?2 or DICT_RETIREMENT_LEVEL = ?2 " +
                "or DICT_TREATMENT_NOW = ?2 or DICT_REVOLUTION_PERIOD = ?2 or DICT_HEALTH_STATUS = ?2 or DICT_MARRIAGE = ?2 or DICT_LIVE_STATU = ?2 " +
                "or DICT_SPOUSE_STATUS = ?2 or DICT_SPOUSE_HEALTH = ?2 or DICT_POLITICAL_STATUS = ?2 or DICT_SUBORDINATE = ?2 or DICT_ALLERGEN_UNIT_NATUREL = ?2  )")
    List<String> getPidByDicPId(Integer isSoftDel, String dicPid);
    
    YjglUser findByUserName(String userName);

}
